package com.asp.aspchain.transaction.elastic;

import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.event.JobEventConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticJobConfig {

	@Bean(initMethod = "init")
	public ZookeeperRegistryCenter regCenter(@Value("${elaticjob.zookeeper.server-lists}") final String serverList, @Value("${elaticjob.zookeeper.namespace}") final String namespace) {
		return new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverList, namespace));
	}


	@Autowired
	private ZookeeperRegistryCenter regCenter;
	@Autowired
	private JobEventConfiguration jobEventConfiguration;

	/**
	 * 动态添加
	 *
	 * @param jobClass
	 * @param cron
	 * @param shardingTotalCount
	 * @param shardingItemParameters    
	 */
	public void addSimpleJobScheduler(final Class<? extends SimpleJob> jobClass,
									  final String cron,
									  final int shardingTotalCount,
									  final String shardingItemParameters) {
		JobCoreConfiguration coreConfig = JobCoreConfiguration.newBuilder(jobClass.getName(), cron, shardingTotalCount).shardingItemParameters(shardingItemParameters).jobParameter("job参数").build();
		SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(coreConfig, jobClass.getCanonicalName());
		JobScheduler jobScheduler = new JobScheduler(regCenter, LiteJobConfiguration.newBuilder(simpleJobConfig).build());
		jobScheduler.init();
	}

	/**
	 * 动态启动定时器，写入数据库，simpleJob对象可注入
	 *
	 * @param simpleJob
	 * @param cron
	 * @param shardingTotalCount
	 * @param shardingItemParameters
	 */

	public void simpleJobScheduler(final SimpleJob simpleJob,
								   final String cron,
								   final int shardingTotalCount,
								   final String shardingItemParameters) {
		JobScheduler jobScheduler = new SpringJobScheduler(simpleJob, regCenter, getLiteJobConfiguration(simpleJob.getClass(), cron, shardingTotalCount, shardingItemParameters), jobEventConfiguration);
		jobScheduler.init();

	}

	private LiteJobConfiguration getLiteJobConfiguration(final Class<? extends SimpleJob> jobClass, final String cron, final int shardingTotalCount, final String shardingItemParameters) {
		return LiteJobConfiguration.newBuilder(new SimpleJobConfiguration(JobCoreConfiguration.newBuilder(
				jobClass.getName(), cron, shardingTotalCount).shardingItemParameters(shardingItemParameters).build(), jobClass.getCanonicalName())).overwrite(true).build();
	}

}